package top.blackr.config;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 工具类
 *
 * @author BlackR
 */
@UtilityClass
public class Utils {

    /**
     * 农历（旧历）出生日期
     *
     * @return 今年年龄
     */
    public Integer getAge(String date) {
        LocalDateTime parse = LocalDateTimeUtil.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        ChineseDate chineseDate = new ChineseDate(parse.getYear(), parse.getMonthValue(), parse.getDayOfMonth());
        // 获取公历时间
        Date birthday = chineseDate.getGregorianDate();
        return DateUtil.age(birthday, new Date());
    }

    /**
     * 生日倒计时
     *
     * @param date 开始时间
     * @return 最近剩余天数
     */
    public Long getCountDown(String date) {
        LocalDateTime parse = LocalDateTimeUtil.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        ChineseDate chineseDate = new ChineseDate(LocalDateTime.now().getYear(), parse.getMonthValue(), parse.getDayOfMonth());
        // 获取公历时间
        Date birthday = chineseDate.getGregorianDate();
        long countDown = DateUtil.between(birthday, new Date(), DateUnit.DAY);
        if (countDown < 0) {
            chineseDate = new ChineseDate(LocalDateTime.now().getYear() + 1, parse.getMonthValue(), parse.getDayOfMonth());
            // 获取公历时间
            birthday = chineseDate.getGregorianDate();
            countDown = DateUtil.between(birthday, new Date(), DateUnit.DAY);
        }
        return countDown;
    }

    public static void main(String[] args) {
        System.out.println(getAge("1996-10-15"));
        System.out.println(getCountDown("1996-10-15"));
    }
}
